package com.jgsu.farm.controller;

import com.jgsu.farm.entity.User;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.GeneralUserService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: farm-10-14-2019
 * @description: 普通用户信息管理
 * @author: Mr.zuo
 * @create: 2019-12-06 17:49
 **/
@RestController
@Slf4j
@RequestMapping(value = "farm/sysuser/user/generaluser")
public class GeneralUserController {

    @Autowired
    GeneralUserService generalUserService;

    /**
     * 分页查询所有普通用户列表
     * */
       @GetMapping(produces = "application/json")
       public CommonResult<PageInfo<User>> QueryGeneral(@RequestParam(value = "page",required = false,defaultValue = "1")int page,@RequestParam(value = "pagesize",required = false,defaultValue = "10") int pagesize) {
           try {
               PageInfo<User> user = generalUserService.queryGeneral(3,page,pagesize);
               log.info("查询第{}页所有普通用户",page);
               return CommonResult.success(user);
           } catch (DataNotExistException e) {
               log.error("第{}页普通用户信息查询失败",page);
               throw new DataNotExistException(e.getCode(), e.getMessage());
           }
       }

    /**
     * 对普通用户数据进行修改
     * */
      @PutMapping(value = "/update", produces = "application/json")
      public CommonResult modifyGeneral(User user) {
          try {
              generalUserService.modifyGeneralMsg(user);
              log.info("根据phone{}修改该用户信息",user.getPhone());
              return CommonResult.success(user, "ok");
          }catch (DataNotExistException e){
              throw new DataNotExistException(e.getCode(),e.getMessage());
          }
      }

    /**
     * 删除普通用户数据
     * */
       @DeleteMapping(value = "/delete", produces = "application/json")
       public CommonResult deleteGeneralByPhone(String phone) {
           int data = generalUserService.deleteGeneralByPhone(phone);
           if (data != 1){
               return CommonResult.failed(ResultCode.DATA_Not_Exist_ERROR);
           }else {
               return CommonResult.success(ResultCode.SUCCESS);
           }
       }
}
